package mysql

import (
	"gorm.io/gorm"
	"newgoods/newgoodsrpc/newgoodsrpc"
)

type Goods struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(30);not null;comment:商品名称"`
	Price   float64 `gorm:"type:decimal(10,2);not null;comment:商品价格"`
	Stock   int     `gorm:"type:int;not null;comment:商品库存"`
	Image   string  `gorm:"type:varchar(255);not null;comment:商品图片"`
	ClassId int     `gorm:"type:int;not null;comment:商品ID"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(&g).Error
}

func (g *Goods) FindGoodsByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(&g).Error
}
func GoodsList(db *gorm.DB, list []*newgoodsrpc.GoodsList) []*newgoodsrpc.GoodsList {
	db.Debug().Model(&Goods{}).
		Select(
			"goods.id as goods_id",
			"goods.name as goods_name",
			"goods.price",
			"goods.stock",
			"goods.image",
			"classes.name as class_name",
			"goods.created_at",
		).
		Joins("left join classes  on classes.id=goods.class_id ").Find(&list)
	return list
}
